#!/usr/bin/env python

import string
import re

def soundex(name, len=4):
    """ soundex module conforming to Knuth's algorithm

        implementation 2000-12-24 by Gregory Jorgensen
        public domain
        http://code.activestate.com/recipes/52213/
    """

    # digits holds the soundex values for the alphabet
    digits = '01230120022455012623010202'
    sndx = ''
    fc = ''

    # translate alpha chars in name to soundex digits
    for c in name.upper():
        if c.isalpha():
            if not fc: fc = c   # remember first letter
            d = digits[ord(c)-ord('A')]
            # duplicate consecutive soundex digits are skipped
            if not sndx or (d != sndx[-1]):
                sndx += d

    # replace first digit with first alpha character
    sndx = fc + sndx[1:]

    # remove all 0s from the soundex code
    sndx = sndx.replace('0','')

    # return soundex code padded to len characters
    return (sndx + (len * '0'))[:len]

def main(a):
    return soundex(a)

if __name__ == '__main__':
    """ """
    print soundex("Presovsky Viktor , Umelecka 618/7", len=4)
    print soundex("Victor Presowsky", len=4)
    print soundex("Viktor Presowsky", len=4)
    print soundex("Victor Presovsky", len=4)
    print soundex("Viktor", len=4)
    print soundex("Presovsky", len=4)
    print soundex("NOVAK", len=4)
    print soundex("NINAJ", len=4)

    print soundex("Presovsky Viktor , Umelecka 618/7", len=10)
    print soundex("Victor Presowsky", len=10)
    print soundex("Viktor Presowsky", len=10)
    print soundex("Victor Presovsky", len=10)
    print soundex("Viktor", len=10)
    print soundex("Presovsky", len=10)
    print soundex("NOVAK", len=10)
    print soundex("NINAJ", len=10)
    pass